package com.sanjiahantang.youjiarole.module.decorate.views.impl;

import android.app.Dialog;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.zoomimage.MyViewPager;
import com.sanjiahantang.uilibrary.views.zoomimage.PicViewPagerAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.decorate.presenters.DecorateManagePresenter;
import com.sanjiahantang.youjiarole.module.decorate.views.DecorateManageView;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by zy on 2017/11/25.
 */

public class CheckPicActivity extends BaseMvpActivity<DecorateManageView,DecorateManagePresenter> implements DecorateManageView, View.OnClickListener {


    private GridView grid;
    private int priId;
    private QuickAdapter<FileBean.Data> adapter;
    private Dialog dialog;
    private PicViewPagerAdapter picViewPagerAdapter;
    private TextView txtEvaluate;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public DecorateManagePresenter initPresenter() {
        return new DecorateManagePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_grid);
        grid = (GridView) findViewById(R.id.grid);
        txtEvaluate = (TextView) findViewById(R.id.txtEvaluate);
        TextView title = (TextView) findViewById(R.id.title);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        priId = getIntent().getIntExtra(KEY_ID,0);
        if (priId == 0) {
            Utils.showToast($this,"获取步骤失败");
            finish();
            return;
        }
        int padding = Utils.dip2px($this,15);
        grid.setPadding(padding,padding,padding,padding);
        title.setText(getIntent().getStringExtra(KEY_TITLE) == null ? "监理验收" : getIntent().getStringExtra(KEY_TITLE));
        String eva;
        if (TextUtils.isEmpty(eva = getIntent().getStringExtra(KEY_PARAM))){
            txtEvaluate.setVisibility(View.GONE);
        }else{
            txtEvaluate.setVisibility(View.VISIBLE);
            txtEvaluate.setText(eva);
        }
        adapter = new QuickAdapter<FileBean.Data>($this,R.layout.item_image) {
            @Override
            protected void convert(BaseAdapterHelper helper, FileBean.Data item) {
                ImageView iv = helper.getView(R.id.image);
                Picasso.with($this).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).error(R.drawable.i_rect_bg).into(iv);
            }
        };

        grid.setAdapter(adapter);

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                picViewPagerAdapter.setmDirPath(FileUtils.rePath(adapter.getItem(i).getUrl()));
                picViewPagerAdapter.notifyDataSetChanged();
                dialog.show();
            }
        });

        dialog = new Dialog($this,R.style.dialog_common);
        picViewPagerAdapter = new PicViewPagerAdapter($this,"");
        picViewPagerAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        final MyViewPager viewPager = new MyViewPager($this);
        viewPager.setAdapter(picViewPagerAdapter);
        dialog.setContentView(viewPager);
    }

    @Override
    public void initData() {
        presenter.searchFieldList($this,priId, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof List){
                    List<FileBean.Data> list = (List<FileBean.Data>) content;
                    adapter.clear();
                    adapter.addAll(list);
                }
            }
        });
    }

    @Override
    public DecorateManagePresenter getPresenter() {
        return presenter;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
